﻿using EasyShop.IdentityServer.Repository.Domain;
using IdentityServer4.Test;
using Infrastructure;
using System;
using System.Linq;
using System.Security.Claims;

namespace EasyShop.IdentityServer.App
{
    public class UserApp
    {
        private readonly MySqlDBContext _mySqlDBContext;
        public UserApp(MySqlDBContext mySqlDBContext)
        {
            _mySqlDBContext = mySqlDBContext;
        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TestUser FindByUsername(string userName)
        {
            var user = _mySqlDBContext.User.Where(c => c.UserName == userName).FirstOrDefault();
            var testUser = new TestUser()
            {
                Username = user.UserName,
                SubjectId = user.Id,
                ProviderName = user.UserName,
                Password = user.Password,
                Claims = new[]
                {
                    new Claim("name", user.UserName),
                    new Claim("website", user.UserName)
                }
            };
            return testUser;
        }
        public User Get(string id)
        {
            var user = _mySqlDBContext.User.Where(c => c.Id == id).FirstOrDefault();
            return user;
        }
        public bool ValidateCredentials(string username,string password)
        {
            var exists = _mySqlDBContext.User.Where(c => c.UserName == username && c.Password==password);
            if (exists.Count() == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
